<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never" style="border:0px;">
      <div slot="header" style="font-size:14px;font-weight:bold;">
        <span>房抵贷台账</span>
      </div>
      <!-- 查询和其他操作 -->
      <div class="top">
        <p style="font-size: 16px;">当前机构进件数据统计明细</p>
        <p style="padding-left: 30px;">
          <span style="padding-right: 10px;">截止日期</span>
          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOption"
          >
          </el-date-picker>
        </p>
        <p style="padding-left: 30px;">
          <span style="padding-right: 10px;">所属机构</span>
            <el-select v-model="orgId" clearable  placeholder="请选择" style="width:22.5%">
              <el-option
                v-for="(item, index) in orgList"
                :key="index"
                :label="item.orgName"
                :value="item.id"
              />
            </el-select>
        </p>
      </div>

      <vxe-toolbar ref="xToolbar">
        <template #buttons>
          <el-button
            v-loading="loading"
            type="primary"
            icon="el-icon-download"
            @click="downloadExcel"
            :disabled="loading"
          >下载运营数据台账</el-button>
          <span class="subTitle">大约需要0~10s</span>
        </template>
      </vxe-toolbar>
    </el-card>
  </div>
</template>

<script>
import { downloadExcel } from '@/api/account';
import { allOrg } from '@/api/org';
import { mapGetters } from 'vuex';
export default {
  name: 'list',
  data() {
    return {
      loading:false,
      orgId: undefined,
      orgList: [],
      date:[],
      pickerOption: {
        disabledDate(current) {
          return current && current.getTime() > new Date().getTime();
        }
      }
    };
  },
  computed: {
    ...mapGetters(['roles']),
  },
  mounted() {},
  created() {
    if (this.roles.indexOf('客户经理') != -1) {
      this.isCustomerManager = true;
    }
    this.listOrgs()
  },
  methods: {
        // 查询所有机构
    listOrgs() {
      allOrg().then((data) => {
        this.orgList = data.data;
      });
    },
    downloadExcel() {
      if(this.date.length == 0) {
        this.$message.error('请选择截止日期')
        return false
      }
      if(this.orgId == undefined) {
        this.$message.error('请选择所属机构')
        return false
      }
        this.loading = true
        downloadExcel({startDate:this.date[0],endDate:this.date[1],orgId:this.orgId})
          .then(res => {
            const blob = new Blob([res]) ;
            const fileName = "进件台账.xlsx"
             if (window.navigator.msSaveOrOpenBlob) { //兼容IE10
                navigator.msSaveBlob(blob,fileName)
              } else {
                const href = URL.createObjectURL(blob) //创建新的URL表示指定的blob对象
                const a = document.createElement('a') //创建a标签
                a.style.display = 'none'
                a.href = href // 指定下载链接
                a.download = fileName //指定下载文件名
                a.click() //触发下载
                URL.revokeObjectURL(a.href) //释放URL对象
              }
              this.$notify.success({
                title: '成功',
                message: '下载成功'
              })
              this.loading = false;
          }).catch(err => {
            this.$notify.error({
                title: '失败',
                message: err.data.message
              })
          })
    }
  },
};
</script>

<style lang="css" scoped>
  .top p{
    padding: 0 0 20px 0;
    margin :0;
  }
  .subTitle{
    vertical-align:bottom;
    padding-left: 20px;
    color:#999;
  }
</style>
